using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DrinkDebugController : MonoBehaviour
{
	private ZoomAtBarController zoomAtBarController;

	private DrinkTargets drinkTargets;

	private LiquidPicker liquidPicker;

	private GlassFillingUpController glassFillingUpController;

	private string glassPrefabPath = "Prefabs/Glasses/";

	private GameObject glassObject;

	private Transform glassSpawnPoint;

	private float zoomedOutGlassScale = 0.412f;

	public Dropdown dropdown;

	private List<string> drinkNames;

	private int currentDropdownValue;

	private void Awake()
	{
		DefineConnections();
		drinkNames = new List<string>();
		LoadLevelsToObtainAllDrinks();
		FillDropdown();
	}

	private void DefineConnections()
	{
		zoomAtBarController = GetComponent<ZoomAtBarController>();
		glassSpawnPoint = base.transform.FindDeepChild("GlassSpawnPoint");
		liquidPicker = GetComponent<LiquidPicker>();
	}

	private IEnumerator LoadDrink(string drinkName)
	{
		INIParser iniParser = new INIParser();
		TextAsset file = Resources.Load<TextAsset>("DataFiles/drinks");
		iniParser.Open(file);
		string glassName = glassPrefabPath + "Glass_" + iniParser.ReadValue(drinkName, "glass_id", 1);
		GameObject glassPrefab = Resources.Load(glassName) as GameObject;
		GameObject glassInstance = UnityEngine.Object.Instantiate(glassPrefab, glassSpawnPoint.position, default(Quaternion));
		glassInstance.transform.localScale = new Vector3(zoomedOutGlassScale, zoomedOutGlassScale, 1f);
		glassObject = glassInstance;
		drinkTargets = glassInstance.GetComponent<DrinkTargets>();
		zoomAtBarController.glass = new Transform[1]
		{
			glassInstance.transform
		};
		iniParser.Close();
		zoomAtBarController.ZoomAtBar();
		liquidPicker.SetUpConnectionsWithGlassObject(glassObject);
		glassFillingUpController = glassObject.GetComponent<GlassFillingUpController>();
		yield return new WaitForSeconds(1.2f);
		SetTargetLines(iniParser, drinkName);
	}

	private void SetTargetLines(INIParser iniParser, string sectionName)
	{
		List<LiquidType> list = new List<LiquidType>();
		List<float> list2 = new List<float>();
		string arg = "liquid_";
		int num = 1;
		string text = arg + num;
		string key = text + "_percentage";
		while (iniParser.IsKeyExists(sectionName, text))
		{
			string name = iniParser.ReadValue(sectionName, text, string.Empty);
			float item = iniParser.ReadValue(sectionName, key, 0);
			LiquidType liquidType = LiquidTypes.GetLiquidType(name);
			list.Add(liquidType);
			list2.Add(item);
			num++;
			text = arg + num;
			key = text + "_percentage";
		}
		for (int i = 0; i < list.Count; i++)
		{
			float previousLiquidLevel = 0f;
			float nextLiquidLevel = 200f;
			if (i > 0)
			{
				previousLiquidLevel = list2[i - 1];
			}
			if (i + 1 < list.Count)
			{
				nextLiquidLevel = list2[i + 1];
			}
			drinkTargets.SetTarget(list[i], list2[i], previousLiquidLevel, nextLiquidLevel);
		}
	}

	private void LoadLevelsToObtainAllDrinks()
	{
		INIParser iNIParser = new INIParser();
		TextAsset name = Resources.Load<TextAsset>("DataFiles/levels");
		iNIParser.Open(name);
		for (int i = 1; iNIParser.IsSectionExists("level_" + i); i++)
		{
			for (int j = 1; iNIParser.IsKeyExists("level_" + i, "order_" + j); j++)
			{
				string item = iNIParser.ReadValue("level_" + i, "order_" + j, string.Empty);
				if (!drinkNames.Contains(item))
				{
					drinkNames.Add(item);
				}
			}
		}
		iNIParser.Close();
	}

	private void FillDropdown()
	{
		dropdown.AddOptions(drinkNames);
	}

	public void OnDropdownValueChange(Dropdown change)
	{
		currentDropdownValue = change.value;
		UnityEngine.Object.Destroy(glassObject);
		StartCoroutine(LoadDrink(drinkNames[currentDropdownValue]));
	}

	public void OnButtonClicked()
	{
		List<TargetLine> targetLinesList = drinkTargets.GetTargetLinesList();
		foreach (TargetLine item in targetLinesList)
		{
			liquidPicker.LiquidPicked(LiquidTypes.GetLiquidName(item.liquidType));
			GlassFillingUpController obj = glassFillingUpController;
			Vector3 position = item.transform.position;
			obj.FillUpGlassPerfectly(position.y);
			glassFillingUpController.FillUpGlass(0f);
		}
	}
}
